package su.mvc.service.gui;


import com.googlecode.objectify.Key;
import com.googlecode.objectify.NotFoundException;
import org.apache.commons.lang.StringUtils;
import su.mvc.model.MenuItem;
import su.mvc.model.PageContent;
import su.mvc.service.DaoSupport;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class MenuService extends DaoSupport{
    
    public static Long TOP_MENU_ITEM = 1L;
    public static Long TOP_PAGE_ITEM = 1L;

    private static final Logger log = Logger.getLogger(MenuService.class.getName());


    public MenuItem getTOP() {
        return getObjectify().get(MenuItem.class, TOP_MENU_ITEM);
    }
    
    public MenuItem getMenuItems() {
        MenuItem top = getTOP();
        parseMenuTree(top);
        return top;
    }
    
    private void parseMenuTree(MenuItem currentTop){
        List<MenuItem> children = getObjectify().query(MenuItem.class).ancestor(currentTop).order("order").list();
        for(MenuItem item: children){
            if (!currentTop.getKey().equals(item.getKey()) && currentTop.getKey().equals(item.getParentItemKey())) {
                currentTop.addChildItem(item);
                parseMenuTree(item);
            }
        }
    }
    
    public Key saveMenuItem(MenuItem menuItem){
        if (menuItem.getParentItemKey() == null && !getTOP().getKey().equals(menuItem.getKey())) {
            menuItem.setParentItemKey(getTOP().getKey());
        }
        Key<MenuItem> key;
        if (menuItem.getKey() == null)  {
            key = getObjectify().put(menuItem);
            menuItem.setKey(key);
        } else {
            if (!menuItem.getKey().equals(getTOP().getKey()))  {
                MenuItem savedMenu = (MenuItem)getObjectify().get(menuItem.getKey());
                getObjectify().delete(savedMenu);
                menuItem.setKey(null);
                key = getObjectify().put(menuItem);
                menuItem.setKey(key);
            } else {
                menuItem.setParentItemKey(null);
                key = menuItem.getKey();
            }
        }
        String name= menuItem.getName();
        String content = menuItem.getContent();
        menuItem = (MenuItem)getObjectify().find(menuItem.getKey());
        menuItem.setName(name);
        getObjectify().put(menuItem);
        PageContent pageContent;
        if (menuItem.getContentId() != null) {
            pageContent = getObjectify().get(PageContent.class, menuItem.getContentId());
        } else {
            pageContent = createPageContent(menuItem);
        }
        pageContent.setContent(content);
        getObjectify().put(pageContent);
        return key;
    }
    
    public void removeMenuItem(Key key){
        MenuItem menuItem = (MenuItem)getObjectify().get(key);
        PageContent pageContent = getObjectify().get(PageContent.class, menuItem.getContentId());
        getObjectify().delete(pageContent);
        getObjectify().delete(key);
    }
    
    public MenuItem getMenuItemByKey(Key<MenuItem> key) {
        MenuItem menuItem =  getObjectify().get(key);
        PageContent pageContent;
        if (menuItem.getContentId() != null) {
            pageContent = getObjectify().get(PageContent.class, menuItem.getContentId());
        } else {
            pageContent = createPageContent(menuItem);
        }
        menuItem.setContent(pageContent.getContent());
        return menuItem;
    }
    
    public PageContent createPageContent(MenuItem menuItem) {
        PageContent pageContent = new PageContent();
        getObjectify().put(pageContent);
        menuItem.setContentId(pageContent.getId());
        getObjectify().put(menuItem);
        return pageContent;
    }
    
    public Map<String, String> getMenuItemsMap(String placeholder) {
        Map<String, String> menuMap = new LinkedHashMap<String, String>();
        parseMenuTreeForMap(getMenuItems(), menuMap, 0, placeholder);
        return menuMap;
        
    }

    private void parseMenuTreeForMap(MenuItem currentTop, Map<String, String> menuMap, Integer level, String placeholder){
        List<MenuItem> children = currentTop.getChildItems();
        for(MenuItem item: children){
            String name = StringUtils.repeat(placeholder, level*4) + item.getName();
            menuMap.put(item.getKey().getString(),name);
            parseMenuTreeForMap(item, menuMap, level+1, placeholder);
        }
    }
}
